/**
 *******************************************************************************
 * @file can_task.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @date 2025-05-10
 *
 * @brief Project: AK601
 *
 * @copyright Copyright (c) 2025 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_TASK_H
#define __CAN_TASK_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/**
 * @brief User CAN Protocol
 *
 */
#define  CAN_ID_VCU                     (0x0AAU)
#define  CAN_ID_VCU_KEYS                (0x407U)
#define  CAN_ID_AiDU_STATE              (0x501U)
#define  CAN_ID_AiDU_VSTATE             (0x503U)
#define  CAN_ID_BMS_STATE               (0x111U)

/** @brief CAN 矩阵
 */

/** 0x0AA : VCU拨杆状态
 *                                 MSB   LSB  Signal Length
 * Stick1              摇杆信号1    7     0         8
 * Stick2              摇杆信号2    15    8         8
 * SupplyVoltage       电源电压     23    16        8
 * RollingCounter      循环计数器   55    52        4
 */

typedef union {
    uint64_t DualWord;
    uint8_t Byte[8U];

    struct {
        uint64_t Stick1           : 8U;
        uint64_t Stick2           : 8U;
        uint64_t SupplyVoltage    : 8U;
        uint64_t Reserved1        : 28U; //!< bit 24 - bit 51
        uint64_t RollingCounter   : 4U;
        uint64_t Reserved2        : 8U;
    } Bits_VCU;

    struct {
        uint64_t KEY_ONE          : 2U;  /*!< 一键开关:
                                            - 0x00:Invalid
                                            - 0x1:触发信号 */
        uint64_t KEY_INDICATOR    : 2U;  /*!< 转向灯:
                                            - 0x0:关闭转向灯
                                            - 0x1:开左转灯
                                            - 0x2:开右转灯
                                            - 0x3:开双跳灯 */
        uint64_t KEY_LOWHIGHBEAM  : 1U;  /*!< 远近光切换:
                                            - 0x0:近光灯
                                            - 0x1:远光灯*/
        uint64_t KEY_SPEED        : 2U;  /*!< 最大速度:
                                            - 0x0:Invalid
                                            - 0x1:低速
                                            - 0x2:中速
                                            - 0x3:高速 */
        uint64_t Reserved1        : 1U;  //!< bit 7
        uint64_t KEY_GEAR         : 3U;  /*!< 空挡档位:
                                            - 0x00:Invalid
                                            - 0x01:N（仅发送这个）
                                            - 0x02:R
                                            - 0x03:D
                                            - 0x04:P */
        uint64_t Reserved2        : 21U; //!< bit 11 - bit 31
        uint64_t KEY_TRIGGER      : 1U;  /*!< 按键信号:
                                            - 0x00:Invalid
                                            - 0x1:Valid */
        uint64_t KEY_SPEAKER      : 1U;  /*!< 喇叭信号:
                                            - 0x00:Invalid
                                            - 0x1:Valid */
        uint64_t Reserved3        : 30U; //!< bit 34 - bit 63

    } Bits_VCU_KEYS;

    struct {
        uint64_t DRIVE_STATE      : 4U; /*!< 驾驶状态:
                                            - 0x0:人驾
                                            - 0x1:遥控
                                            - 0x2:召唤
                                            - 0x3:回充
                                            - 0x4:跟随
                                            - 0x5:部署
                                            - 0x6:故障 */
        uint64_t Reserved1        : 60U; //!< bit 4 - bit 63

    } Bits_AiDU_STATE;

    struct {
        uint64_t AiDU_SPEED       : 8U; //!< 速度
        uint64_t AiDU_GEAR        : 3U; /*!< 目标档位:
                                            - 0x00: Invalid
                                            - 0x01: N
                                            - 0x02: R
                                            - 0x03: D
                                            - 0x04: P */
        uint64_t AiDU_INDICATOR   : 2U; /*!< 转向灯:
                                            - 0x0:关闭转向灯
                                            - 0x1:开左转灯
                                            - 0x2:开右转灯
                                            - 0x3:开双跳灯 */
        uint64_t AiDU_LOWHIGHBEAM : 1U; /*!< 远近光切换:
                                            - 0x0:近光灯
                                            - 0x1:远光灯*/

    } Bits_AiDU_VSTATE;

    struct {
        uint64_t BMS_SOC_H        : 8U; //!< 动力电池SOC *0.1%
        uint64_t BMS_SOH_H        : 4U; //!< 动力电池SOH *0.1%
        uint64_t BMS_SOC_L        : 4U; //!< 动力电池SOC *0.1%
        uint64_t BMS_SOH_L        : 8U; //!< 动力电池SOH *0.1%
        uint64_t Reserved1        : 16U; //!< bit 24 - bit 39
        uint64_t BMS_WIRELESS_CHR : 3U; /*!< BMS无线充电状态:
                                            - 0x0:未连接
                                            - 0x1:待机（已连接,充电待机）
                                            - 0x2:正在充电
                                            - 0x3:已充满
                                            - 0x4:停止充电
                                            - 0x5:禁止充电
                                            - 0x6:耦合度检测 */

    } Bits_BMS_STATE;

} User_CanMsgTx_t;

/* Exported macro ------------------------------------------------------------*/
#define CAN_STB_LOW()     CAN1_STB_LOW()
#define CAN_STB_HIGH()    CAN1_STB_HIGH()

/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
void uCAN_DeInit(void);
void uCAN_Init(void);
void uCAN_Handle(void);
void uCAN_TimerCallback(void);


#ifdef __cplusplus
}
#endif

#endif
/* __CAN_TASK_H */
